package com.wf.smartcomm.server.serviceImpl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Call;
import com.wf.smartcomm.server.service.CallService;

@Service
@Transactional
public class CallServiceImpl implements CallService {
	
	@Resource
	private SessionFactory sessionFactory;

	@Override
	public void save(Call call) throws Exception {
		sessionFactory.getCurrentSession().persist(call);
	}

	@Override
	public void delete(String id) throws Exception {
		sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Call.class, id));
	}

	@Override
	public Call getCaller(String called) throws Exception {
		String sql = "select *from t_call where called = :called order by createdAt desc limit 0,1";
		Query query = sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity(Call.class);
		query.setParameter("called", called);
		return (Call) query.uniqueResult();
	}

}
